const pool = require('../config/database');

// 创建新用户
const createUser = async (userData) => {
    try {
        const { username, password, role } = userData;
        const [result] = await pool.execute(
            'INSERT INTO users (username, password, role) VALUES (?, ?, ?)',
            [username, password, role]
        );
        return result.insertId;
    } catch (error) {
        throw error;
    }
};

// 根据用户名查找用户
const getUserByUsername = async (username) => {
    try {
        const [rows] = await pool.execute(
            'SELECT * FROM users WHERE username = ?',
            [username]
        );
        return rows[0];
    } catch (error) {
        throw error;
    }
};

// 根据邮箱查找用户
const getUserByEmail = async (email) => {
    try {
        const [rows] = await pool.execute(
            'SELECT * FROM users WHERE email = ?',
            [email]
        );
        return rows[0];
    } catch (error) {
        throw error;
    }
};

// 获取所有用户
const getAllUsers = async () => {
    try {
        const [rows] = await pool.execute(
            'SELECT id, username, role, created_at, updated_at FROM users'
        );
        return rows;
    } catch (error) {
        throw error;
    }
};

// 根据 ID 更新用户信息
const updateUserById = async (id, updateData) => {
    try {
        const { username, email, password, role } = updateData;
        let query = 'UPDATE users SET ';
        const values = [];
        const updates = [];

        if (username) {
            updates.push('username = ?');
            values.push(username);
        }
        if (email) {
            updates.push('email = ?');
            values.push(email);
        }
        if (password) {
            updates.push('password = ?');
            values.push(password);
        }
        if (role) {
            updates.push('role = ?');
            values.push(role);
        }

        if (updates.length === 0) {
            return false;
        }

        query += updates.join(', ') + ', updated_at = CURRENT_TIMESTAMP WHERE id = ?';
        values.push(id);

        const [result] = await pool.execute(query, values);
        return result.affectedRows > 0;
    } catch (error) {
        throw error;
    }
};

// 更新用户状态
const updateUserStatus = async (id, status) => {
    try {
        const [result] = await pool.execute(
            'UPDATE users SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
            [status, id]
        );
        return result.affectedRows > 0;
    } catch (error) {
        throw error;
    }
};

// 根据 ID 删除用户
const deleteUserById = async (id) => {
    try {
        const [result] = await pool.execute(
            'DELETE FROM users WHERE id = ?',
            [id]
        );
        return result.affectedRows > 0;
    } catch (error) {
        throw error;
    }
};

// 根据ID获取用户
const getUserById = async (id) => {
    try {
        const [rows] = await pool.execute(
            'SELECT id, username, role, created_at, updated_at FROM users WHERE id = ?',
            [id]
        );
        return rows[0];
    } catch (error) {
        throw error;
    }
};

module.exports = {
    createUser,
    getUserByUsername,
    getUserByEmail,
    getAllUsers,
    updateUserById,
    updateUserStatus,
    deleteUserById,
    getUserById
};